<?php
    namespace Home\Controller;
    use Think\Controller;
    class QitajixiaoshezhiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $st_date = I('post.st_date');
        if(empty($st_date))
        {
            $st_date = I('get.st_date');
            if(empty($st_date))
            {
                die();
            }
        }
        $st_date = strtotime($st_date);
        
        // $data = M("performance.set_dept_bonuspercent")->field("st_date,dept_code")->limit(($page-1)*$limit,$limit)->group('st_date,dept_code')->select();
        
        
        $data = M("comm.sys_dept_dict")->where("attr = 1")->limit(($page-1)*$limit,$limit)->select();
        
        
        for ($i=0; $i < count($data); $i++) 
        { 
            $data[$i]['st_date'] = $st_date;
            
        }
        
        $bonusguide_dict_list = M("performance.set_bonusguide_dict")->select();
        
        
        for ($i=0; $i < count($data); $i++) 
        { 
            for ($j=0; $j < count($bonusguide_dict_list); $j++)
            { 
                $data[$i]['num'.$bonusguide_dict_list[$j]['bonusguide_id']] = 0;
                $data[$i]['id'.$bonusguide_dict_list[$j]['bonusguide_id']] = $bonusguide_dict_list[$j]['bonusguide_id'];
                $data[$i]['name'.$bonusguide_dict_list[$j]['bonusguide_id']] = $bonusguide_dict_list[$j]['bonusguide_name'];
                $data[$i]['list'][] = $bonusguide_dict_list[$j]['bonusguide_id'];
            }
        }

        $st_dates = "";
        $dept_codes = "";
        
        for ($i=0; $i < count($data); $i++) 
        { 
            $st_dates .= "'".$data[$i]['st_date']."'";
            $st_dates .= ",";
            $dept_codes .= "'".$data[$i]['dept_code']."'";
            $dept_codes .= ",";
        }
        $st_dates   =  rtrim($st_dates,",");
        $dept_codes =  rtrim($dept_codes,",");
        

        $all_data = M("performance.set_dept_bonuspercent")->where("st_date in ( $st_dates ) and dept_code in ( $dept_codes ) and st_date = '$st_date'")->select();
        
        for ($i=0; $i < count($data); $i++) 
        { 
            for ($j=0; $j < count($all_data); $j++) 
            { 
                if( $data[$i]["dept_code"] == $all_data[$j]["dept_code"] && $data[$i]["id".$all_data[$j]['bonusguide_id']] == $all_data[$j]["bonusguide_id"])
                {
                    $data[$i]["num".$all_data[$j]['bonusguide_id']] = $all_data[$j]["guidepercent"];
                }
            }
        }

        
        // $a = M("performance.set_dept_bonuspercent")->field("st_date,dept_code")->group('st_date,dept_code')->select();
        

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("comm.sys_dept_dict")->where("attr = 1")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("performance.set_dept_bonuspercent")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("performance.set_dept_bonuspercent")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("performance.set_dept_bonuspercent")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
        $data_all = I("post.");

        $dept_code    = $data_all["dept_code"];
        $st_date      = $data_all["st_date"];
        $id           = $data_all["id"];
        $num          = $data_all["num"];

        $where['dept_code'] = $dept_code;
        $where['st_date'] = $st_date;
        $where['bonusguide_id'] = $id;

        $data['guidepercent'] = $num;

        $info = M("performance.set_dept_bonuspercent")->where($where)->find();
        if(empty($info))
        {
            $data['dept_code'] = $dept_code;
            $data['st_date'] = $st_date;
            $data['bonusguide_id'] = $id;
            $result = M("performance.set_dept_bonuspercent")->where($where)->add($data);
        }
        else
        {
            $result = M("performance.set_dept_bonuspercent")->where($where)->save($data);
        }
        
        
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");
    }

}